package co.edu.militar.multimedia.frame;

import co.edu.militar.multimedia.admin.ArbolManager;
import co.edu.militar.multimedia.admin.FileManager;
import java.io.File;
import java.util.UUID;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;

/**
 *
 * @author Alejandro
 */
public class ImageManagerFrame extends javax.swing.JFrame {

    private final ArbolManager am;

    /**
     * Creates new form ImageManagerFrame
     */
    public ImageManagerFrame() {
        am = new ArbolManager();
        initComponents();
        arbol.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        selector = new javax.swing.JFileChooser();
        jScrollPane1 = new javax.swing.JScrollPane();
        arbol = new JTree(am.getRoot());
        jScrollPane2 = new javax.swing.JScrollPane();
        imageLabel = new javax.swing.JLabel();
        labelCrear = new javax.swing.JLabel();
        labelEliminar = new javax.swing.JLabel();
        botonEliminar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        botonInsertar = new javax.swing.JButton();
        botonDestruir = new javax.swing.JButton();
        labelDestruir = new javax.swing.JLabel();
        botonCrear = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        selector.setMultiSelectionEnabled(true);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Administrador de imagenes");
        setBackground(new java.awt.Color(0, 0, 0));

        arbol.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 3, true));
        arbol.setAutoscrolls(true);
        arbol.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                arbolValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(arbol);

        jScrollPane2.setBackground(new java.awt.Color(255, 255, 255));
        jScrollPane2.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 3, true));

        imageLabel.setLabelFor(jScrollPane2);
        jScrollPane2.setViewportView(imageLabel);

        labelCrear.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N
        labelCrear.setLabelFor(botonCrear);
        labelCrear.setText("Crear");

        labelEliminar.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N
        labelEliminar.setText("Eliminar");

        botonEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/militar/multimedia/frame/img/borrar.png"))); // NOI18N
        botonEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonEliminarActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N
        jLabel3.setText("Insertar");

        botonInsertar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/militar/multimedia/frame/img/insertar.png"))); // NOI18N
        botonInsertar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonInsertarActionPerformed(evt);
            }
        });

        botonDestruir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/militar/multimedia/frame/img/destruir.png"))); // NOI18N
        botonDestruir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonDestruirActionPerformed(evt);
            }
        });

        labelDestruir.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N
        labelDestruir.setText("Destruir");

        botonCrear.setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/militar/multimedia/frame/img/crear.png"))); // NOI18N
        botonCrear.setActionCommand("botonCrear");
        botonCrear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCrearActionPerformed(evt);
            }
        });

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(102, 102, 102)
                                .addComponent(labelEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(labelCrear, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(botonCrear)
                            .addComponent(botonEliminar))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(labelDestruir, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(botonDestruir))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(botonInsertar)))
                        .addGap(103, 103, 103))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 700, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 424, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(botonCrear)
                            .addComponent(labelCrear, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(15, 15, 15)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(botonEliminar)
                            .addComponent(labelEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(botonInsertar)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(botonDestruir)
                            .addComponent(labelDestruir, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(14, Short.MAX_VALUE))
            .addComponent(jScrollPane1)
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void botonCrearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCrearActionPerformed
        File f = new File("C:", UUID.randomUUID().toString() + ".png");
        am.addNodo(f.getAbsolutePath(), arbol);
    }//GEN-LAST:event_botonCrearActionPerformed

    private void botonInsertarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonInsertarActionPerformed
        try {
            for (File f : FileManager.cargarArchivos(selector)) {
                am.addNodo(f.getAbsolutePath(), arbol);
            }
        } catch (NullPointerException ex) {

        }
    }//GEN-LAST:event_botonInsertarActionPerformed

    private void arbolValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_arbolValueChanged
        DefaultMutableTreeNode nseleccionado = (DefaultMutableTreeNode) arbol.getLastSelectedPathComponent();
        if (nseleccionado != null) {
            ImageIcon icono = new ImageIcon(nseleccionado.toString());
            imageLabel.setIcon(icono);
            imageLabel.setSize(0, 0);
        } else {
            imageLabel.setIcon(null);
            imageLabel.setSize(0, 0);
        }
    }//GEN-LAST:event_arbolValueChanged

    private void botonEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonEliminarActionPerformed
        DefaultMutableTreeNode nseleccionado = (DefaultMutableTreeNode) arbol.getLastSelectedPathComponent();
        if (nseleccionado != null) {
            am.deleteNodo(nseleccionado, arbol);
        }
    }//GEN-LAST:event_botonEliminarActionPerformed

    private void botonDestruirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonDestruirActionPerformed
        am.limpiarNodo(arbol);
    }//GEN-LAST:event_botonDestruirActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTree arbol;
    private javax.swing.JButton botonCrear;
    private javax.swing.JButton botonDestruir;
    private javax.swing.JButton botonEliminar;
    private javax.swing.JButton botonInsertar;
    private javax.swing.JLabel imageLabel;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel labelCrear;
    private javax.swing.JLabel labelDestruir;
    private javax.swing.JLabel labelEliminar;
    private javax.swing.JFileChooser selector;
    // End of variables declaration//GEN-END:variables
}
